package com.yuantu.judiciary.serviceimpl;

import cn.hutool.http.HttpRequest;
import com.google.gson.Gson;
import com.yuantu.judiciary.common.SysCode;
import com.yuantu.judiciary.exception.ServiceException;
import com.yuantu.judiciary.model.api_wx.AccessTokenDTO;
import com.yuantu.judiciary.model.api_wx.GetUserInfoParam;
import com.yuantu.judiciary.model.api_wx.UserOpenIdInfoDTO;
import com.yuantu.judiciary.model.api_wx.UserPhoneInfoDTO;
import com.yuantu.judiciary.service.IApiWxAppletService;
import com.yuantu.judiciary.utils.AssertUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 微信小程序
 *
 * @author syw
 * @since 2023-08-09
 **/
@Service
@RequiredArgsConstructor
public class ApiWxAppletServiceImpl implements IApiWxAppletService {

    private static final Gson GSON = new Gson();
    @Value("${applet.appId}")
    private String appId;
    @Value("${applet.appSecret}")
    private String appSecret;


    @Override
    public UserPhoneInfoDTO getUserPhoneInfo(GetUserInfoParam param) throws ServiceException {
        try {
            String tokenBody = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret).execute().body();
            AssertUtil.assertTrue(StringUtils.isNotBlank(tokenBody), SysCode.WX_APPLET_API_TOKEN_ERROR);
            AccessTokenDTO accessToken = GSON.fromJson(tokenBody, AccessTokenDTO.class);
            AssertUtil.assertTrue(StringUtils.isNotBlank(accessToken.getAccessToken()), SysCode.WX_APPLET_API_TOKEN_ERROR);
            String body = GSON.toJson(param);
            String response = HttpRequest.post("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken.getAccessToken()).body(body).execute().body();
            AssertUtil.assertTrue(StringUtils.isNotBlank(response), SysCode.WX_APPLET_API_PHONE_ERROR);
            return GSON.fromJson(response, UserPhoneInfoDTO.class);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.WX_APPLET_API_ERROR);
        }
    }

    @Override
    public UserOpenIdInfoDTO getUserOpenIdInfo(GetUserInfoParam param) throws ServiceException {
        try {
            String response = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + param.getCode() + "&grant_type=authorization_code").execute().body();
            AssertUtil.assertTrue(StringUtils.isNotBlank(response), SysCode.WX_APPLET_API_OPENID_ERROR);
            UserOpenIdInfoDTO userOpenIdInfo = GSON.fromJson(response, UserOpenIdInfoDTO.class);
            return userOpenIdInfo;
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new ServiceException(SysCode.WX_APPLET_API_ERROR);
        }
    }


}
